package com.mycompany.virtualApp.controller;

import com.mycompany.utils.DateUtil;
import com.mycompany.virtualApp.service.TopService;
import com.mysql.jdbc.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * 全局的接口
 */

@Controller
@RequestMapping("/systemControll")
public class TopController {
    @Autowired
    TopService topService;
    /**
     * 关闭桌面广告显示
     * value 0 显示  1不显示
     */
    @RequestMapping("/closeDesktopAdv")
    @ResponseBody
    public void closeDesktopAdv(@RequestParam(value = "colseTime", required = false) String closeTime) {
        Date date = millis2Date(System.currentTimeMillis());
        HashMap<String, Object> hashMap = new HashMap();
        hashMap.put("code", "desktopAdvShow");
        hashMap.put("valueName", "1");
        hashMap.put("del_flag", "0");
        hashMap.put("closeTime", date);
        topService.updateDesktopAdvInfo(hashMap);
    }

    /**
     * 操作tab广告栏 显示 或者隐藏  value 0显示  value 1 隐藏
     */

    @RequestMapping("/operateTabAdvInfo")
    @ResponseBody
    public void operateTabAdvInfo(@RequestParam(value = "value", required = false) String value) {
        HashMap map = new HashMap();
        map.put("valueName", value);
        map.put("code", "tabAdvShow");
        topService.operateTabAdvInfo(map);
    }
    /**
     * 记录广告的点击次数  type 0 是 banner广告 1是桌面广告  2是tab广告   id是用来确定是哪个广告的
     */
    @RequestMapping("/updateAdvClickNums")
    @ResponseBody
    public void recordDevClickNum(@RequestParam(value = "type", required = false) String type,
                                  @RequestParam(value = "id", required = false) Integer id){
        HashMap map = new HashMap();
        map.put("code", "tabAdvShow");
        map.put("id", id);
        if (StringUtils.equals("0",type)){
            //banner广告
            topService.updateBannerClickNums(map);
        }else if (StringUtils.equals("1",type)){
            //桌面广告
            topService.updateDesktopClickNums(map);
        }else {
            //tab广告
            topService.updateTabClickNums(map);
        }

    }

    /**
     * 将时间戳转为Date类型
     *
     * @param millis 毫秒时间戳
     * @return Date类型时间
     */
    public static Date millis2Date(final long millis) {
        return new Date(millis);
    }

}
